package com.example.myapplication01.service.tinno.ipc.messenger;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.myapplication01.R;
import com.example.myapplication01.databinding.ActivityMessengerBinding;
import com.example.myapplication01.service.tinno.service.MyService;

public class MessengerActivity extends AppCompatActivity {

    private ActivityMessengerBinding binding;
    private Messenger messenger;
    private MyConn myConn;
    private boolean boundFlag;
    private Messenger receiveReplyMsg= new Messenger(new ReceiveHandler());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding = ActivityMessengerBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.messenger), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        if (myConn == null) {
            myConn = new MyConn();
        }

        Intent intent = new Intent(this, MyService.class);
        bindService(intent, myConn, BIND_AUTO_CREATE);

        binding.stopBtn.setOnClickListener(v -> unbindService(myConn));
        binding.callBtn.setOnClickListener(v -> {
            if (!boundFlag) return;
            try {
                Message msg = Message.obtain(null, 1, 0, 0);
                msg.replyTo = receiveReplyMsg;
                messenger.send(msg);
            } catch (RemoteException e) {
                throw new RuntimeException(e);
            }
        });


    }

    // 接受消息处理的handler
    class ReceiveHandler extends Handler {
        public void handleMessage(Message msg) {
            // 接收服务端回复
            switch (msg.what) {
                case 1:
                    Log.i("MessengerActivity", "receiver message from service:" + msg.getData().getString("reply"));
                    break;
                default:
                    super.handleMessage(msg);
            }
        }
    }

    class MyConn implements ServiceConnection {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            messenger = new Messenger(service);
            boundFlag = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            messenger = null;
        }
    }
}